//
//  AJTTItemAddStepTwoController.swift
//  JourneyThroughTime
//
//  Created by iOS-Czz on 2023/11/6.
//

import UIKit
import ProgressHUD
class AJTTItemAddStepTwoController: AJTTBaseViewController {
    @IBOutlet var picture: UIImageView!
    var model = AJTTItemModel()
    @IBOutlet var bgView: UIView!
    var modernPic:UIImage? = nil

    @IBOutlet var descTV: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.descTV.addPlaceholder("Please enter modern appearance description", color: .lightGray)
        // Do any additional setup after loading the view.
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.bgView.addRoundCorners(topLeftRadius: 15, topRightRadius: 15, bottomLeftRadius: 0, bottomRightRadius: 0, borderWidth: 0, borderColor: .red)
    }
    @IBAction func clickBackAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: false)
    }
    @IBAction func clickSaveAction(_ sender: Any) {
    }
    @IBAction func clickNextAction(_ sender: Any) {
        if self.modernPic == nil {
            ProgressHUD.showError("Please select modern picture")
            return
        }
        if self.descTV.text!.isEmpty {
            ProgressHUD.showError("Please enter modern appearance description")
            return
        }
        model.modernPic = UIImage.saveImageInCache(self.modernPic!)!
        model.modernDesc = self.descTV.text!
        let nextVC = AJTTItemAddStepThreeController()
        nextVC.model = self.model
        self.navigationController?.pushViewController(nextVC, animated: false)
    }
    
    @IBAction func tapSelectModernPictureAction(_ sender: Any) {
        showPickImageVC()
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
extension AJTTItemAddStepTwoController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func showPickImageVC() {
        let photoPickerViewController:UIImagePickerController = UIImagePickerController()
        photoPickerViewController.sourceType = UIImagePickerController.SourceType.photoLibrary
        photoPickerViewController.delegate = self
        photoPickerViewController.modalPresentationStyle = .fullScreen
        self.present(photoPickerViewController, animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        self.picture.image = selectedImage
        self.modernPic = selectedImage
        picker.dismiss(animated: true, completion: nil)
    }
}
